package com.finance.action.android.customer;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.Deduction;
import com.common.vo.JsonVo;
import com.finance.entity.Balance;
import com.finance.entity.BalanceRecord;
import com.finance.entity.customer.LocationalRecord;
import com.finance.service.BalanceRecordService;
import com.finance.service.BalanceService;
import com.finance.service.customer.LocationalRecordService;

@Controller("LocationalRecordAction")
public class LocationalRecordAction {

	@Resource(name = "LocationalRecordService")
	private LocationalRecordService locationalRecordService;
	
	@Resource(name = "BalanceService")
	private BalanceService balanceService;
	
	@Resource(name = "BalanceRecordService")
	private BalanceRecordService balanceRecordService;

	/**
	 * 定位记录
	 * @param kfUserId 客户id
	 * @param userId 业务员id（因为要扣费，所以需要业务员id）
	 * @param adds 地址
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
//	@RequestMapping(value = "addlocationalRecord", method = RequestMethod.POST)
	@RequestMapping("addlocationalRecord")
	public JsonVo<String> addlocationalRecord(
			@RequestParam("kfUserId") String kfUserId,
			@RequestParam("userId") String userId,
			@RequestParam("adds") String adds,HttpServletRequest request) throws UnsupportedEncodingException {
		JsonVo<String> json = new JsonVo<String>();
		if (kfUserId.equals("")) {
			json.setMsg("用户id为空");
			return json;
		}
		if (userId.equals("")) {
			json.setMsg("业务员id为空");
			return json;
		}
		if (adds.equals("")) {
			json.setMsg("地址为空");
			return json;
		}
		LocationalRecord locationalRecord = new LocationalRecord();
		String kfUserId1 = request.getParameter("kfUserId");
	    String kfUserId2=new String(kfUserId1.getBytes("ISO-8859-1"),"UTF-8");
		locationalRecord.setKfUserId(kfUserId2);
		String adds1 = request.getParameter("adds");
	    String adds2=new String(adds1.getBytes("ISO-8859-1"),"UTF-8");
		locationalRecord.setAdds(adds2);
		locationalRecord.setCreationTime(new Date());
		locationalRecordService.insert(locationalRecord);
		
		Balance balance = balanceService.selectByPrimaryKey(userId);
		if (balance == null) {
			json.setMsg("当前用户没有余额记录，请检查业务员id是否正确");
			return json;
		}
		Long moy = balance.getBalanceCount() - Deduction.Location;
		if (moy < 0) {
			json.setCode(1);
			json.setMsg("余额不足定位失败");
			return json;
		}
		balance.setBalanceCount(moy);
		balanceService.updateByPrimaryKeySelective(balance);
		
		BalanceRecord balanceRecord = new BalanceRecord();
		balanceRecord.setUserId(balance.getUserId());
		balanceRecord.setRdTime(new Date());
		balanceRecord.setRdComment("授权定位消费");
		balanceRecord.setRdType(2);
		balanceRecord.setRdValue(Deduction.Location);
		balanceRecordService.insert(balanceRecord);
		json.setCode(200);
		json.setMsg("成功");
		json.setSuccess(true);
		return json;
	}

}
